import {ADD_TODO, SET_VISIBILITY_FILTER, TOGGLE_TODO, VisibilityFilters} from './actions';
import {combineReducers} from 'redux';

const {SHOW_ALL} = VisibilityFilters;

function visibilityFilter(state = SHOW_ALL,action) {
    switch (action.type) {
        case SET_VISIBILITY_FILTER:
            return action.filter;
            break;
        default:
            return state;
            break;
    }
}

function todos(state = [],action) {
    switch (action.type) {
        case ADD_TODO:
            return [
                {
                    text:action.text,
                    completed:false
                }
            ]
            break;
        case TOGGLE_TODO:
            return state.map((todo,index)=>{
                if (index === todo.index) {
                    return Object.assign({},todo,{
                        completed:!todo.completed
                    })
                }
                return todo;
            })
            break;
        default:
            return state
            break;
    }
}

const toDoApp = combineReducers({
    visibilityFilter,
    todos
})

export default toDoApp;
